//
//  SearchAreas.m
//  LocomotiveGuards
//
//  Created by zwj on 15-12-12.
//  Copyright (c) 2015年 REDOOR. All rights reserved.
//

#import "SearchAreas.h"
@implementation SearchAreas
- (instancetype)init
{
    if (self = [super init]) {
        _provinceArray = [NSMutableArray array];
        [self parseXml];
    }
    return self;
}

- (void)parseXml{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"province_data" ofType:@"xml"];
    if (path.length == 0) {
        NSLog(@"没有读到资源!");
        return;
    }
    
    NSData *xmlData = [NSData dataWithContentsOfFile:path];
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:nil];
    NSArray *pArray = [document nodesForXPath:@"/root/province" error:nil];
    
  
    
    for (GDataXMLElement *element in pArray) {
        NSString *provinceName = [[element attributeForName:@"name"] stringValue];
        NSArray *cArray = [element elementsForName:@"city"];
        
//          NSLog(@"获取最新的省市区   %@",cArray);
        
        
        NSMutableArray *tempCityArr = [NSMutableArray array];
        for (GDataXMLElement *item in cArray) {
            NSString *cityName = [[item attributeForName:@"name"] stringValue];
            NSArray *dArray = [item elementsForName:@"district"];
            NSMutableArray *tempDistArr = [NSMutableArray array];
            for (GDataXMLElement *sub in dArray) {
                NSString *distName = [[sub attributeForName:@"name"] stringValue];
                NSString *zipcode = [[sub attributeForName:@"zipcode"] stringValue];
                NSArray *lpsArray = [sub elementsForName:@"localpolicestation"];//localpolicestation
                NSMutableArray *tempLpsArr = [NSMutableArray array];
//                NSLog(@"活的省市区所有的数据    %@",lpsArray);
                
                
                for (GDataXMLElement *lps in lpsArray) {
                    NSString *lpsName = [[lps attributeForName:@"name"] stringValue];
                    [tempLpsArr addObject:lpsName];
                }
                
                NSDictionary *dic = [NSDictionary dictionaryWithObjects:@[distName,zipcode,tempLpsArr] forKeys:@[@"dName",@"zipcode",@"lpsArr"]];
                [tempDistArr addObject:dic];
            }
            
            NSDictionary *dic = [NSDictionary dictionaryWithObjects:@[cityName,tempDistArr] forKeys:@[@"cName",@"distArr"]];
            [tempCityArr addObject:dic];   
        }
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjects:@[provinceName,tempCityArr] forKeys:@[@"pName",@"cityArr"]];
        [_provinceArray addObject:dic];
    }
}

//获取所有的省
- (NSArray *)returnProvinces
{
    NSMutableArray *tempProArr = [NSMutableArray array];
    for (NSInteger i=0; i<_provinceArray.count; i++) {
        [tempProArr addObject:[_provinceArray[i] objectForKey:@"pName"]];
    }
    NSArray *proNameArray = [tempProArr copy];
    return proNameArray;
}

//获取某个省下所有的市
- (NSArray *)returnCitysWithProvince:(NSString *)provinceName
{
    NSArray *cityArray = [NSArray array];
    NSMutableArray *tempCityArr = [NSMutableArray array];
    for (NSInteger i=0; i<_provinceArray.count; i++) {
        if ([[_provinceArray[i] objectForKey:@"pName"] isEqualToString:provinceName])
        {
            cityArray = [_provinceArray[i] objectForKey:@"cityArr"];
            break;
        }
    }
    for (NSInteger i=0; i<cityArray.count; i++) {
        [tempCityArr addObject:[cityArray[i] objectForKey:@"cName"]];
    }
    NSArray *cityNameArray = [tempCityArr copy];
    return cityNameArray;
}

//获取某个市下所有的区
- (NSArray *)returnDistrictsWithProvince:(NSString *)provinceName City:(NSString *)cityName
{
    NSArray *districtArray = [NSArray array];
    NSMutableArray *tempDistArr = [NSMutableArray array];
    for (NSInteger i=0; i<_provinceArray.count; i++) {
        NSArray *cArray = [NSArray array];
        cArray = [_provinceArray[i] objectForKey:@"cityArr"];
        for (NSInteger j=0; j<cArray.count; j++) {
            if ([[_provinceArray[i] objectForKey:@"pName"] isEqualToString:provinceName] &&
                [[cArray[j] objectForKey:@"cName"] isEqualToString:cityName])
            {
                districtArray = [cArray[j] objectForKey:@"distArr"];
                break;
            }
        }
    }
    for (NSInteger i=0; i<districtArray.count; i++) {
        [tempDistArr addObject:[districtArray[i] objectForKey:@"dName"]];
    }
    NSArray *distNameArray = [tempDistArr copy];
    return distNameArray;
}

//获取某个区下所有的派出所
- (NSArray *)returnAddressWithProvince:(NSString *)provinceName City:(NSString *)cityName District:(NSString *)districtName
{
    NSArray *lpsArray = [NSArray array];
    for (NSInteger i=0; i<_provinceArray.count; i++) {
        NSArray *cArray = [NSArray array];
        cArray = [_provinceArray[i] objectForKey:@"cityArr"];
        for (NSInteger j=0; j<cArray.count; j++) {
            NSArray *dArray = [NSArray array];
            dArray = [cArray[j] objectForKey:@"distArr"];
            for (NSInteger k=0; k<dArray.count; k++) {
                if ([[_provinceArray[i] objectForKey:@"pName"] isEqualToString:provinceName] &&
                    [[cArray[j] objectForKey:@"cName"] isEqualToString:cityName] &&
                    [[dArray[k] objectForKey:@"dName"] isEqualToString:districtName])
                {
                    lpsArray = [dArray[k] objectForKey:@"lpsArr"];
                    break;
                }
            }
        }
    }
    return lpsArray;
}
@end
